package com.emser.unc.server.domain;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Sequence;
import javax.jdo.annotations.SequenceStrategy;

import com.emser.unc.serializable.ProductDTO;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Sequence(name = "seqp", strategy = SequenceStrategy.NONTRANSACTIONAL,extensions = @Extension(vendorName = "datanucleus", key="key-cache-size", value="12"))
public class Product {
	@PrimaryKey
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private Long code;
	@Persistent
	private String title;
	@Persistent
	private Double unitPrice;
	@Persistent
	private Long stock;
	@Persistent
	private UserAccount userA;

	public Long getCode() {
		return code;
	}
	public void setCode() {
		this.code = key.getId();
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public Double getUnitPrice() {
		return unitPrice;
	}
	
	public void setUnitPrice(Double unitPrice) {
		this.unitPrice = unitPrice;
	}
	
	public Long getStock() {
		return stock;
	}
	
	public void setStock(Long stock) {
		this.stock = stock;
	}
	
	public Key getKey() {
		return key;
	}
	
	public static ProductDTO toDTO(Product product) {
	    if (product == null) {
	      return null;
	    }
	    ProductDTO productDTO = new ProductDTO();
	    productDTO.setCode(product.getCode());
	    productDTO.setTitle(product.getTitle());
	    productDTO.setUnitPrice(product.getUnitPrice());
	    productDTO.setStock(product.getStock());
	    return productDTO;
	  }
	
	public UserAccount getUserA() {
		return userA;
	}

}
